import store from '@/store/store'
import { fileUrl } from '@/util/util'
import wx from 'weixin-js-sdk'
import { initConfig, checkStatus } from '@/model/wxSDK/index'

function programShare() {
  const { userState } = store
  return () => {
    const qr_code = userState.userToken.qr_code
    return {
      title: '生态管理系统',
      // imageUrl: "分享图标",
      // content: "分享文案",
      path: `/pages/home3/index?qr_code=${qr_code || ''}`,
    }
  }
}

function gamesShare(type: string = '') {
  // #ifdef H5
  setTimeout(() => {
    const options = _gamesShare()
    wx.showMenuItems({
      menuList: ['menuItem:share:appMessage', 'menuItem:share:timeline'],
    })
    wx.updateAppMessageShareData(options)
    wx.updateTimelineShareData(options)
  }, 1000)
  return () => ({})
  // #endif

  // #ifdef MP-WEIXIN
  return () => {
    const activityId = JSON.parse(uni.getStorageSync('activityId') || '{}')
    const { act_id, type } = activityId
    return {
      title: '互动悦读',
      // imageUrl: "分享图标",
      // content: "分享文案",
      path: `/pGames/gameLoading/index?act_id=${act_id || ''}&type=${type || ''}`,
    }
  }
  // #endif
}
function _gamesShare() {
  const activityId = JSON.parse(uni.getStorageSync('activityId') || '{}')
  const title = uni.getStorageSync('shareTitle')
  const title2 = uni.getStorageSync('shareTitle2')
  const imgUrl = uni.getStorageSync('shareImg')
  const { act_id, type } = activityId
  const link = `${window.location.origin}/gy_lib3/wx/pGames/gameLoading/index?act_id=${act_id || ''}&type=${
    type || ''
  }&t=${Date.now()}`
  return {
    title: title || '阅享活动',
    link,
    desc: title2 || '',
    imgUrl: fileUrl(imgUrl) || fileUrl('default/default_share.png'),
  }
}

// #ifdef H5
async function dftShare(opt = {}) {
  await checkStatus()
  const options = {
    title: document.title || '生态管理系统',
    desc: '',
    link: window.location.href,
    imgUrl: fileUrl('default/default_share.png'),
    ...opt,
  }
  console.log(options, 'options')
  wx.showMenuItems({
    menuList: ['menuItem:share:appMessage', 'menuItem:share:timeline'],
  })
  wx.updateAppMessageShareData(options)
  wx.updateTimelineShareData(options)
}
// #endif
// #ifdef MP-WEIXIN
function dftShare(){}
// #endif

export { dftShare, gamesShare, programShare }
